import WeCropper from '../../ccmini-cmpts/lib/we-cropper/we-cropper.js'

const ccminiPageHelper = require('../../helper/ccmini_page_helper.js');
const device = wx.getSystemInfoSync()
const width = device.windowWidth
const height = device.windowHeight - 50

var base = require('../../base.js');  //路径可能做相应调整
const _ = base._; //翻译函数


Page({
    data: {
        language: "中文",
        cropperOpt: {
            id: 'cropper',
            targetId: 'targetCropper',
            pixelRatio: device.pixelRatio,
            width,
            height,
            src: '',
            scale: 2.5,
            zoom: 8,
            cut: {
                x: (width - 250) / 2,
                y: (height - 250) / 2,
                width: 250,
                height: 250
            },
            boundStyle: {
                color: '#fff',
                mask: 'rgba(0,0,0,0.8)',
                lineWidth: 1
            }
        }
    },

    onLoad: async function (options) {
        this.setData({
            _t: base._t(), //翻译
        });
        const {
            cropperOpt
        } = this.data;
        // cropperOpt.src = "";
        this.cropper = new WeCropper(cropperOpt)
            .on('ready', (ctx) => {

            })
            .on('beforeImageLoad', (ctx) => {
                wx.showToast({
                    title: '上传中',
                    icon: 'loading',
                    duration: 3000
                });
            })
            .on('imageLoad', (ctx) => {
                wx.hideToast();
            })
            .on('beforeDraw', (ctx, instance) => {
            })
    },

    url: function (e) {
        ccminiPageHelper.url(e, this);
    },

    touchStart: function (e) {
        this.cropper.touchStart(e);
    },
    touchMove: function (e) {
        this.cropper.touchMove(e);
    },
    touchEnd: function (e) {
        this.cropper.touchEnd(e);
    },

    //提交图片
    bindCropperImageTap: async function () {
        this.cropper.getCropperImage(async function (path, err) {
            if (err) {
                console.log(err)
                wx.showToast({
                    title: '图片裁剪失败',
                    icon: 'error',
                    duration: 3000
                });
            } else {
                let prevPage = ccminiPageHelper.getPrevPage();
                // 直接调用上一个页面的setData()方法，把数据存到上一个页面中去
                prevPage.setData({
                    userPic: path,
                })
                wx.navigateBack({
                    delta: 0,
                });
            }
        })
    },

    uploadTap: function () {
        console.log("打开相册")
        let that = this;
        wx.chooseImage({
            count: 1, // 默认9
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) {
                const src = res.tempFilePaths[0];
                //  获取裁剪图片资源后，给data添加src属性及其值
                that.cropper.pushOrign(src);
            }
        })
    },

})
